﻿@using FwProject.Core.Distribution
@using FwProject.Distribution.Dto
@using FwProject.Core.Distribution
@using FwProject.Authorization
@model Tuple<DistributionDetailsView, List<DistributionDetailsDto>>
@{
    ViewBag.Title = "配送单[" + Model.Item1.CreationNumber + "]";
    int SumDistributioncount = 0;
    int SumRecoverycount = 0;
    int SumExchangegoodscount = 0;
    int SumDamagecount = 0;
    int SumGivecount = 0;
    int SumBackboxcount = 0;
    int SumBacknumbercount = 0;
    decimal SumPrice = 0;
}
@section styles{
    <link href="~/Theme/css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="~/Theme/css/plugins/datapicker/datepicker3.css" rel="stylesheet">
    <style type="text/css">
        .input-group[class*=col-] {
            padding: 0 15px;
        }
    </style>
}
<div class="row wrapper wrapper-content animated fadeInRight">
    <div class="col-sm-12">
        <div class="ibox float-e-margins">
            <div class="ibox-title">
                <h5>@ViewBag.Title</h5>
                <div class="ibox-tools">
                    <a class="btn btn-primary btn-xs" href="javascript:top.window.openTab(7556,'操作历史[@Model.Item1.CreationNumber]','/Distribution/History?id=@Model.Item1.Id')">历史</a>
                    <button type="button" class="btn btn-info btn-xs" onclick="window.open('/Distribution/Print?id=@Model.Item1.Id','_blank','width=996,height=666')"><i class="glyphicon glyphicon-print"></i>打印</button>
                </div>
            </div>
            <div class="ibox-content" style="height:700px">
                <div class="row m-b-sm m-t-sm">
                    <form class="form-horizontal" asp-controller="Distribution" asp-action="EditDistributionDeltails" data-ajax="true" data-ajax-update="#data_list" data-ajax-method="Post" data-ajax-mode="replace" data-ajax-success="ReloadAjaxCallbackgoback" data-ajax-failure="FailureAjaxCallback">
                        <div class="form-group">
                            <input type="hidden" name="id" value="@Model.Item1.Id" />

                            <div class="col-md-4">
                                <label class="control-label col-md-3">单号：</label>
                                <div class="input-group">
                                    <input type="text" name="CreationNumber" class="input-sm form-control" value="@Model.Item1.CreationNumber" readonly="readonly">
                                </div>
                            </div>
                            <div class="col-md-4">
                                <label class="control-label col-md-3">配送日期：</label>
                                <div class="input-group">
                                    <input type="text" name="CreationTime" class="input-sm form-control" value="@Model.Item1.CreationTime.ToString("yyyy-MM-dd HH:mm:ss")" >
                                </div>
                            </div>
                            <div class="col-md-4">
                                <label class="control-label col-md-3">餐厅：</label>
                                <div class="input-group">
                                    <input type="hidden" name="Restaurantid" value="@Model.Item1.RestaurantId" />
                                    <input type="text" name="RestaurantName" class="input-sm form-control" value="@Model.Item1.RestaurantName" readonly="readonly">
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-md-4">
                                <label class="control-label col-md-3">司机：</label>
                                <div class="input-group">
                                    <input type="hidden" name="CreatorUserId" value="@Model.Item1.CreatorUserId" />
                                    <input type="text" name="DriverName" class="input-sm form-control" value="@Model.Item1.Name" readonly="readonly">
                                </div>
                            </div>
                            <div class="col-md-4">
                                <label class="control-label col-md-3">车牌号：</label>
                                <div class="input-group">
                                    <input type="text" name="PlatesNumber" class="input-sm form-control" value="@Model.Item1.PlatesNumber" readonly="readonly">
                                </div>
                            </div>
                            <div class="col-md-4">
                                <label class="control-label col-md-3">应收金额：</label>
                                <div class="input-group">
                                    <input type="text" name="TotalPrice" id="TotalPrice" class="input-sm form-control" value="@Model.Item1.TotalPrice" readonly="readonly">
                                </div>
                            </div>
                            <input type="hidden" name="SettlementId" value="@(Model.Item1.SettlementId.HasValue ? Model.Item1.SettlementId.Value : Guid.Empty)" />
                            <input type="hidden" name="Pricestatus" value="@Model.Item1.Pricestatus" />
                        </div>
                        <div class="form-group">
                            <div class="col-md-4">
                                <label class="control-label col-md-3">店存(应存)：</label>
                                <div class="input-group">
                                    <input type="text" name="DistributionStock" id="DistributionStock" class="input-sm form-control" value="@Model.Item1.DistributionStock" readonly="readonly">

                                    <input type="hidden" name="RestaurantStock" id="RestaurantStock" value="@(Model.Item1.DistributionStock-(Model.Item2.Sum(s=>s.Distributioncount+s.Givecount-s.Backboxcount-s.Recoverycount)))" />
                                </div>
                            </div>
                            <div class="col-md-4">
                                <label class="control-label col-md-3">店存(实存)：</label>
                                <div class="input-group">
                                    @if (Model.Item1.Pricestatus == Pricestatus.已制单 || Model.Item1.Pricestatus == Pricestatus.已结账)
                                    {
                                        <input type="text" name="Stock" id="Stock" class="input-sm form-control" value="@Model.Item1.Stock" readonly="readonly">
                                    }
                                    else
                                    {
                                        <input type="text" name="Stock" id="Stock" class="input-sm form-control" value="@Model.Item1.Stock">
                                    }
                                </div>
                            </div>
                        </div><hr />
                        <div id="data_list">
                            @if (IsGranted(PermissionNames.Distribution_Edit) && (Model.Item1.Pricestatus == Pricestatus.已配送 || Model.Item1.Pricestatus == Pricestatus.未结账 || Model.Item1.Pricestatus == Pricestatus.制单退回))
                            {
                                <div>
                                    @if (Model.Item2.Count == 0)
                                    {
                                        <h4>暂无数据</h4>
                                    }
                                    else
                                    {
                                        <table class="table table-bordered table-hover table-rowselected" id="tab">
                                            <thead>
                                                <tr>
                                                    <th>货品型号</th>
                                                    <th>本次配送箱数</th>
                                                    <th>回收箱数</th>
                                                    <th>换货箱数</th>
                                                    <th>折损套数</th>
                                                    <th>赠送箱数</th>
                                                    <th>退回箱数</th>
                                                    <th>退回套数</th>
                                                    <th>状态</th>
                                                    <th>单价（￥）</th>
                                                    <th>应支付金额（￥）</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                @{var i = 0; }
                                                @foreach (var item in Model.Item2)
                                                {
                                                    <tr>
                                                        <td>
                                                            <input type="hidden" name="DetailsInputDtos[@i].Id" value="@item.Id" />
                                                            <input type="hidden" name="DetailsInputDtos[@i].TypeNumberInfoId" value="@item.TypeNumberInfoId" />
                                                            <input type="hidden" name="DetailsInputDtos[@i].Count" value="@item.TypeNumberInfoModel.Count" />
                                                            <input type="text" name="DetailsInputDtos[@i].TypeNumberName" value="@item.TypeNumberInfoModel.Name" class="input-sm form-control" readonly="readonly" />
                                                        </td>
                                                        <td>
                                                            <input type="text" name="DetailsInputDtos[@i].Distributioncount" value="@item.Distributioncount" class="input-sm form-control" oninput="SetPrice(this)" />
                                                        </td>
                                                        <td>
                                                            <input type="text" name="DetailsInputDtos[@i].Recoverycount" value="@item.Recoverycount" class="input-sm form-control" oninput="SetPrice(this)" />
                                                        </td>
                                                        <td>
                                                            <input type="text" name="DetailsInputDtos[@i].Exchangegoodscount" value="@item.Exchangegoodscount" class="input-sm form-control" oninput="SetPrice(this)" />
                                                        </td>
                                                        <td>
                                                            <input type="text" name="DetailsInputDtos[@i].Damagecount" value="@item.Damagecount" class="input-sm form-control" oninput="SetPrice(this)" />
                                                        </td>
                                                        <td>
                                                            <input type="text" name="DetailsInputDtos[@i].Givecount" value="@item.Givecount" class="input-sm form-control" oninput="SetPrice(this)" />
                                                        </td>
                                                        <td>
                                                            <input type="text" name="DetailsInputDtos[@i].Backboxcount" value="@item.Backboxcount" class="input-sm form-control" oninput="SetPrice(this)" />
                                                        </td>
                                                        <td>
                                                            <input type="text" name="DetailsInputDtos[@i].Backnumbercount" value="@item.Backnumbercount" class="input-sm form-control" oninput="SetPrice(this)" />
                                                        </td>
                                                        <td>
                                                            <input type="text" name="DetailsInputDtos[@i].Pricestatus" value="@item.Pricestatus" class="input-sm form-control" readonly="readonly" />
                                                        </td>
                                                        @if (item.TypeNumberPrice == 0)
                                                        {
                                                            <td>
                                                                <input type="text" name="DetailsInputDtos[@i].TypeNumberPrice" value="@item.TypeNumberPrice" class="input-sm form-control" oninput="SetPrice(this)" />
                                                            </td>
                                                        }
                                                        else
                                                        {
                                                            <td>
                                                                <input type="text" name="DetailsInputDtos[@i].TypeNumberPrice" value="@item.TypeNumberPrice" class="input-sm form-control" readonly="readonly"/>
                                                            </td>
                                                        }
                                                        <td>
                                                            <input type="text" name="DetailsInputDtos[@i].Sumprice" id="Sumprice" value="@item.Sumprice" class="input-sm form-control" readonly="readonly" />
                                                            <input type="hidden" name="DetailsInputDtos[@i].Restaurantid" value="@item.Restaurantid" />
                                                            <input type="hidden" name="DetailsInputDtos[@i].RestaurantTypenNumberInfoId" value="@item.RestaurantTypenNumberInfoId" />
                                                        </td>
                                                    </tr>
                                                    SumDistributioncount += item.Distributioncount;
                                                    SumRecoverycount += item.Recoverycount;
                                                    SumExchangegoodscount += item.Exchangegoodscount;
                                                    SumDamagecount += item.Damagecount;
                                                    SumGivecount += item.Givecount;
                                                    SumBackboxcount += item.Backboxcount;
                                                    SumBacknumbercount += item.Backnumbercount;
                                                    SumPrice += item.Sumprice;
                                                    i++;
                                                }
                                                <tr class="footer" style="color:red">
                                                    <td style="color:red;text-align:center">合计</td>
                                                    <td>
                                                        <input type="text" name="SumDistributioncount" id="SDistributioncount" value="@SumDistributioncount" class="input-sm form-control" readonly="readonly" />
                                                    </td>
                                                    <td>
                                                        <input type="text" name="SumRecoverycount" id="SRecoverycount" value="@SumRecoverycount" class="input-sm form-control" readonly="readonly" />
                                                    </td>
                                                    <td>
                                                        <input type="text" name="SumExchangegoodscount" id="SExchangegoodscount" value="@SumExchangegoodscount" class="input-sm form-control" readonly="readonly" />
                                                    </td>
                                                    <td>
                                                        <input type="text" name="SumDamagecount" id="SDamagecount" value="@SumDamagecount" class="input-sm form-control" readonly="readonly" />
                                                    </td>
                                                    <td>
                                                        <input type="text" name="SumGivecount" id="SGivecount" value="@SumGivecount" class="input-sm form-control" readonly="readonly" />
                                                    </td>
                                                    <td>
                                                        <input type="text" name="SumBackboxcount" id="SBackboxcount" value="@SumBackboxcount" class="input-sm form-control" readonly="readonly" />
                                                    </td>
                                                    <td>
                                                        <input type="text" name="SumBacknumbercount" id="SBacknumbercount" value="@SumBacknumbercount" class="input-sm form-control" readonly="readonly" />
                                                    </td>
                                                    <td colspan="2"></td>
                                                    <td>
                                                        <input type="text" name="TSumPrice" id="TSumPrice" value="@SumPrice" class="input-sm form-control" readonly="readonly" />
                                                    </td>
                                                </tr>

                                            </tbody>
                                        </table>

                                    }
                                </div><br />
                                <div class="ibox-tools">
                                    <button type="submit" class="btn btn-primary btn btn-sm">确认提交</button>
                                </div>
                            }
                            else
                            {
                                <div>
                                    @if (Model.Item2.Count == 0)
                                    {
                                        <h4>暂无数据</h4>
                                    }
                                    else
                                    {
                                        <table class="table table-bordered table-hover table-rowselected">
                                            <thead>
                                                <tr>
                                                    <th>货品型号</th>
                                                    <th>本次配送箱数</th>
                                                    <th>回收箱数</th>
                                                    <th>换货箱数</th>
                                                    <th>折损套数</th>
                                                    <th>赠送箱数</th>
                                                    <th>退回箱数</th>
                                                    <th>退回套数</th>
                                                    <th>状态</th>
                                                    <th>单价（￥）</th>
                                                    <th>应支付金额（￥）</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                @foreach (var item in Model.Item2)
                                                {
                                                    <tr>
                                                        <td>@item.TypeNumberInfoModel.Name</td>
                                                        <td>@item.Distributioncount</td>
                                                        <td>@item.Recoverycount</td>
                                                        <td>@item.Exchangegoodscount</td>
                                                        <td>@item.Damagecount</td>
                                                        <td>@item.Givecount</td>
                                                        <td>@item.Backboxcount</td>
                                                        <td>@item.Backnumbercount</td>
                                                        <td>@item.Pricestatus</td>
                                                        <td>@item.TypeNumberPrice</td>
                                                        <td>@item.Sumprice</td>
                                                    </tr>
                                                    SumDistributioncount += item.Distributioncount;
                                                    SumRecoverycount += item.Recoverycount;
                                                    SumExchangegoodscount += item.Exchangegoodscount;
                                                    SumDamagecount += item.Damagecount;
                                                    SumGivecount += item.Givecount;
                                                    SumBackboxcount += item.Backboxcount;
                                                    SumBacknumbercount += item.Backnumbercount;
                                                    SumPrice += item.Sumprice;
                                                }
                                                <tr class="footer">
                                                    <td style="color:red;text-align:center">合计</td>
                                                    <td style="color:red">@SumDistributioncount</td>
                                                    <td style="color:red">@SumRecoverycount</td>
                                                    <td style="color:red">@SumExchangegoodscount</td>
                                                    <td style="color:red">@SumDamagecount</td>
                                                    <td style="color:red">@SumGivecount</td>
                                                    <td style="color:red">@SumBackboxcount</td>
                                                    <td style="color:red">@SumBacknumbercount</td>
                                                    <td colspan="2"></td>
                                                    <td style="color:red">@SumPrice</td>
                                                </tr>
                                            </tbody>
                                        </table>

                                    }
                                </div>
                            }

                        </div>
                    </form>
                </div>

                <br />
                @if (Model.Item1.TotalPrice == 0 && Model.Item2[0].Pricestatus == Pricestatus.未结账)
                {
                    <div class="ibox-tools">
                        <a href="/Distribution/RecalculatePrice?id=@Model.Item1.Id" class="btn btn-primary btn btn-sm">重计金额</a>
                    </div>
                }
            </div>
        </div>
    </div>
</div>
@section scripts{
    <script src="~/Theme/js/content.min.js?v=1.0.0"></script>
    <script src="~/Theme/js/plugins/iCheck/icheck.min.js"></script>
    <script src="~/Theme/js/plugins/datapicker/bootstrap-datepicker.js"></script>
    <script src="~/Theme/js/plugins/suggest/bootstrap-suggest2014.js"></script>
    <script src="~/js/DateComm.js"></script>
    <script>
        $(function () {

            $('#CreationTime').datepicker({
                todayBtn: "linked",
                autoclose: true,
                todayHighlight: true,
                endDate: new Date()

            });
        });

        function SetPrice(obj) {
            var tr = $(obj).parent().parent();
            var Distributioncount = parseInt($(tr).find("td input").eq(4).val());
            var Recoverycount = parseInt($(tr).find("td input").eq(5).val());
            var Givecount = parseInt($(tr).find("td input").eq(8).val());
            var Exchangegoodscount = parseInt($(tr).find("td input").eq(6).val());
            var Damagecount = parseInt($(tr).find("td input").eq(7).val());
            var Backboxcount = parseInt($(tr).find("td input").eq(9).val());
            var Backnumbercount = parseInt($(tr).find("td input").eq(10).val());

            var DistributionStock = parseInt($("#DistributionStock").val());
            var Stock = parseInt($("#Stock").val());

            if (Distributioncount < 0) {
                $(tr).find("td input").eq(4).val(0);
                Distributioncount = 0;
            }
            if (Recoverycount < 0) {
                $(tr).find("td input").eq(5).val(0);
                Recoverycount = 0;
            }
            if (Givecount < 0) {
                $(tr).find("td input").eq(8).val(0);
                Givecount = 0;
            }
            if (Exchangegoodscount < 0) {
                $(tr).find("td input").eq(6).val(0);
                Exchangegoodscount = 0;
            }
            if (Damagecount < 0) {
                $(tr).find("td input").eq(7).val(0);
                Damagecount = 0;
            }
            if (Backboxcount < 0) {
                $(tr).find("td input").eq(9).val(0);
                Backboxcount = 0;
            }
            if (Backnumbercount < 0) {
                $(tr).find("td input").eq(10).val(0);
                Backnumbercount = 0;
            }
            var Count = parseInt($(tr).find("td input").eq(2).val());
            var Price = $(tr).find("td input").eq(12).val();
            $(tr).find("td input").eq(13).val(toFloat(Distributioncount * Count * Price - (Damagecount * Price) - (Backboxcount * Count * Price) - (Backnumbercount * Price)));

            var trCount = $("#tab tbody tr");
            if (trCount.length > 0) {
                var sumPrice = 0;
                var SDistrinbutioncount = 0;
                var SExchangegoodscount = 0;
                var SGiveCount = 0;
                var SDamagecount = 0;
                var SRecoverycount = 0;
                var SBackboxcount = 0;
                var SBacknumbercount = 0;

                for (var i = 0; i < trCount.length - 1; i++) {
                    var price = $(trCount[i]).find("td input").eq(12).val();
                    sumPrice += ($(trCount[i]).find("td input").eq(4).val() * $(trCount[i]).find("td input").eq(2).val() * price - ($(trCount[i]).find("td input").eq(7).val() * price)) - ($(trCount[i]).find("td input").eq(9).val() * $(trCount[i]).find("td input").eq(2).val() * price) - ($(trCount[i]).find("td input").eq(10).val() * price);

                    SDistrinbutioncount += parseInt($(trCount[i]).find("td input").eq(4).val());
                    SExchangegoodscount += parseInt($(trCount[i]).find("td input").eq(6).val());
                    SGiveCount += parseInt($(trCount[i]).find("td input").eq(8).val());
                    SDamagecount += parseInt($(trCount[i]).find("td input").eq(7).val());
                    SRecoverycount += parseInt($(trCount[i]).find("td input").eq(5).val());
                    SBackboxcount += parseInt($(trCount[i]).find("td input").eq(9).val());
                    SBacknumbercount += parseInt($(trCount[i]).find("td input").eq(10).val());
                }
                $('#TotalPrice').val(toFloat(sumPrice));
                $('#TSumPrice').val(toFloat(sumPrice));

                $('#SDistributioncount').val(SDistrinbutioncount);
                $('#SExchangegoodscount').val(SExchangegoodscount);
                $('#SGivecount').val(SGiveCount);
                $('#SDamagecount').val(SDamagecount);
                $('#SRecoverycount').val(SRecoverycount);
                $('#SBackboxcount').val(SBackboxcount);
                $('#SBacknumbercount').val(SBacknumbercount);
                var m = parseInt($("#RestaurantStock").val());
                $('#DistributionStock').val(m + SDistrinbutioncount + SGiveCount - SRecoverycount - SBackboxcount);
                $('#Stock').val($('#DistributionStock').val());
            }
        }

        function ReloadAjaxCallbackgoback(data, successInfo) {
            CommonAjaxCallback(data, function () {
                window.location.reload();
            }, successInfo);
        }

        var toFloat = function (value) {
            value = Math.round(parseFloat(value) * 100) / 100;
            if (value.toString().indexOf(".") < 0) {
                value = value.toString() + ".00";
            }
            return value;
        }
    </script>
}